导航菜单
首页 >  Using  > Type alias, alias template (since C++11)

Type alias, alias template (since C++11)

 C++Compiler supportFreestanding and hostedLanguageStandard libraryStandard library headersNamed requirementsFeature test macros (C++20)Language support libraryConcepts library (C++20)Metaprogramming library (C++11)Diagnostics libraryGeneral utilities libraryStrings libraryContainers libraryIterators libraryRanges library (C++20)Algorithms libraryNumerics libraryLocalizations libraryInput/output libraryFilesystem library (C++17)Regular expressions library (C++11)Concurrency support library (C++11)Execution support library (C++26)Technical specificationsSymbols indexExternal libraries[edit] C++ languageGeneral topicsPreprocessorCommentsKeywordsEscape sequencesFlow controlConditional execution statementsifswitchIteration statements (loops)forrange-for (C++11)whiledo-whileJump statementscontinue - breakgoto - returnFunctionsFunction declarationLambda function expressioninline specifierDynamic exception specifications (until C++17*)noexcept specifier (C++11)Exceptionsthrow-expressiontry blockhandlerNamespacesNamespace declaration  Namespace aliasesTypesFundamental typesEnumeration typesFunction typesClass/struct typesUnion typesSpecifiersconst/volatiledecltype (C++11)auto (C++11)constexpr (C++11)consteval (C++20)constinit (C++20)Storage duration specifiersInitializationDefault-initializationValue-initializationZero-initializationCopy-initializationDirect-initializationAggregate initializationList-initialization (C++11)  Constant initializationReference initializationExpressionsValue categoriesOrder of evaluationOperatorsOperator precedenceAlternative representationsLiteralsBoolean - Integer - Floating-pointCharacter - String - nullptr (C++11)User-defined (C++11)UtilitiesAttributes (C++11)Typestypedef declarationType alias declaration (C++11)CastsImplicit conversionsstatic_castconst_castExplicit conversionsdynamic_castreinterpret_castMemory allocationnew expressiondelete expressionClassesClass declarationConstructorsthis pointerAccess specifiersfriend specifierClass-specific function propertiesVirtual functionoverride specifier (C++11)  final specifier (C++11)explicit (C++11)staticSpecial member functionsDefault constructorCopy constructorMove constructor (C++11)Copy assignmentMove assignment (C++11)DestructorTemplatesClass templateFunction templateTemplate specializationParameter packs (C++11)MiscellaneousInline assemblyHistory of C++[edit] DeclarationsOverviewDeclaration syntaxdecl-specifier-seqDeclaratorConflicting declarationsSpecifierstypedefinlinevirtual function specifierexplicit function specifierfriendconstexpr(C++11)consteval(C++20)constinit(C++20)Storage class specifiersTranslation-unit-local (C++20)class/structunionenumdecltype(C++11)auto(C++11)alignas(C++11)constvolatilePack indexing specifier (C++26)    Elaborated type specifierAttributes (C++11)DeclaratorsReferencePointerArrayBlock declarationsSimple-declaration→Structured binding declaration (C++17)Alias declaration (C++11)Namespace alias definitionusing declarationusing directivestatic_assert declaration (C++11)asm declarationOpaque enum declaration (C++11)Other declarationsNamespace definitionFunction declarationClass template declarationFunction template declarationExplicit template instantiation (C++11)Explicit template specializationLinkage specificationAttribute declaration (C++11)Empty declaration[edit] 

Type alias is a name that refers to a previously defined type (similar to typedef).

Alias template is a name that refers to a family of types.

Contents1 Syntax2 Explanation3 Notes4 Keywords5 Example6 Defect reports7 See also[edit] Syntax

Alias declarations are declarations with the following syntax:

using identifier attr (optional) = type-id ; (1) template

using identifier attr (optional) = type-id ;

(2) template requires constraint

using identifier attr (optional) = type-id ;

(3) (since C++20) attr - optional sequence of any number of attributes identifier - the name that is introduced by this declaration, which becomes either a type name (1) or a template name (2) template-parameter-list - template parameter list, as in template declaration constraint - a constraint expression which restricts the template parameters accepted by this alias template type-id - abstract declarator or any other valid type-id (which may introduce a new type, as noted in type-id). The type-id cannot directly or indirectly refer to identifier. Note that the point of declaration of the identifier is at the semicolon following type-id.[edit] Explanation1) A type alias declaration introduces a name which can be used as a synonym for the type denoted by type-id. It does not introduce a new type and it cannot change the meaning of an existing type name. There is no difference between a type alias declaration and typedef declaration. This declaration may appear in block scope, class scope, or namespace scope.2) An alias template is a template which, when specialized, is equivalent to the result of substituting the template arguments of the alias template for the template parameters in the type-id.templatestruct Alloc {}; templateusing Vec = vector; // type-id is vector Vec v; // Vec is the same as vector

When the result of specializing an alias template is a dependent template-id, subsequent substitutions apply to that template-id:

templateusing void_t = void; templatevoid_t f(); f(); // error, int does not have a nested type foo

The type produced when specializing an alias template is not allowed to directly or indirectly make use of its own type:

templatestruct A; templateusing B = typename A::U; // type-id is A::U templatestruct A { typedef B U; }; B b; // error: B uses its own type via A::U

Alias templates are never deduced by template argument deduction when deducing a template template parameter.

It is not possible to partially or explicitly specialize an alias template.

Like any template declaration, an alias template can only be declared at class scope or namespace scope.

The type of a lambda expression appearing in an alias template declaration is different between instantiations of that template, even when the lambda expression is not dependent.

templateusing A = decltype([] {}); // A and A refer to different closure types(since C++20)[edit] NotesFeature-test macroValueStdFeature__cpp_alias_templates200704L(C++11)Alias templates[edit] Keywords

using

[edit] ExampleRun this code#include #include #include #include  // type alias, identical to// typedef std::ios_base::fmtflags flags;using flags = std::ios_base::fmtflags;// the name 'flags' now denotes a type:flags fl = std::ios_base::dec; // type alias, identical to// typedef void (*func)(int, int);using func = void (*) (int, int); // the name 'func' now denotes a pointer to function:void example(int, int) {}func f = example; // alias templatetemplateusing ptr = T*;// the name 'ptr' is now an alias for pointer to Tptr x; // type alias used to hide a template parametertemplateusing mystring = std::basic_string; mystring str; // type alias can introduce a member typedef nametemplatestruct Container { using value_type = T; }; // which can be used in generic programmingtemplatevoid info(const ContainerT& c){typename ContainerT::value_type T;std::cout

相关推荐: